package com.jhsw.roadsun.module.business.controller.app.user;

import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.util.object.BeanUtils;
import com.jhsw.roadsun.framework.security.core.util.SecurityFrameworkUtils;
import com.jhsw.roadsun.module.business.controller.app.user.vo.*;
import com.jhsw.roadsun.module.business.dal.dataobject.user.BizUserDO;
import com.jhsw.roadsun.module.business.service.user.BizUserService;
import com.jhsw.roadsun.module.system.api.auth.dto.AppAuthDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 用户信息")
@RestController
@RequestMapping("/biz/biz-user")
@Validated
public class AppBizUserController {

    @Resource
    private BizUserService bizUserService;

    @PostMapping("/wechat/login")
    @Operation(summary = "微信 - 登录")
    @PermitAll
    public CommonResult<AppAuthDTO> loginByWechat(@RequestBody @Valid BizUserWechatLoginReqVO loginReqVO) {
        return success(bizUserService.loginByWechat(loginReqVO));
    }

    @PostMapping("/mobile/register")
    @Operation(summary = "短信 - 手机号注册")
    @PermitAll
    public CommonResult<AppAuthDTO> registerByMobile(@RequestBody @Valid BizUserMobileRegisterReqVO mobileRegisterReqVO) {
        return success(bizUserService.registerByMobile(mobileRegisterReqVO));
    }

    @PostMapping("/mobile/login")
    @Operation(summary = "短信 - 手机号登录")
    @PermitAll
    public CommonResult<AppAuthDTO> loginByMobile(@RequestBody @Valid BizUserMobileLoginReqVO mobileLoginReqVO) {
        return success(bizUserService.loginByMobile(mobileLoginReqVO));
    }

    @PostMapping("/reset/password")
    @Operation(summary = "短信 - 重置密码")
    @PermitAll
    public CommonResult<Boolean> resetPassword(@RequestBody @Valid BizUserMobileRegisterReqVO resetPasswordReqVO) {
        bizUserService.resetPassword(resetPasswordReqVO);
        return success(true);
    }

    @GetMapping("/send-sms-code")
    @PermitAll
    @Operation(summary = "短信 - 发送手机验证码")
    @Parameter(name = "type", description = "类型（1注册 2忘记密码 3换绑手机号）", required = true)
    public CommonResult<Boolean> sendSmsCode(@RequestParam("mobile") String mobile,
                                             @RequestParam("type") Integer type) {
        bizUserService.sendSmsCode(mobile, type);
        return success(true);
    }

    @PostMapping("/forgot/password")
    @Operation(summary = "短信 - 忘记密码")
    public CommonResult<Boolean> forgotPassword(@RequestBody @Valid BizUserMobileRegisterReqVO forgotPasswordReqVO) {
        bizUserService.forgotPassword(forgotPasswordReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @PostMapping("/mobile/change")
    @Operation(summary = "短信 - 换绑手机号")
    public CommonResult<Boolean> mobileChange(@RequestBody @Valid BizUserMobileChangeReqVO mobileChangeReqVO) {
        bizUserService.mobileChange(mobileChangeReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "基本 - 更新用户信息")
    public CommonResult<Boolean> updateBizUser(@Valid @RequestBody AppBizUserUpdateReqVO updateReqVO) {
        bizUserService.appUpdateBizUser(updateReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户信息")
    public CommonResult<AppBizUserRespVO> getBizUser() {
        BizUserDO bizUser = bizUserService.getBizUser(SecurityFrameworkUtils.getLoginUserId());
        return success(BeanUtils.toBean(bizUser, AppBizUserRespVO.class));
    }
}